package com.canlihao.ui.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.canlihao.ui.R;


/**
 * <pre>
 *     author : ddzh
 *     e-mail : xxx@xx
 *     time   : 2019/05/22
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class MyFilletRecView extends View {
    private static final String TAG = "MyFilletRecView";
    private int DEFAULT_REC_BG_COLOR = getResources().getColor(R.color.grey);                       //默认圆角矩形内部填充色
    private int DEFAULT_TAXT_COLOR = getResources().getColor(R.color.white);                        //默认文字颜色
    private int DEFAULT_REC_PRESSED_BG_COLOR = getResources().getColor(R.color.colorPrimary);       //默认按下圆角矩形内部填充色
    private int DEFAULT_REC_WIDTH = 200;                                                            //默认控件宽度
    private int DEFAULT_REC_HEIGHT = 60;                                                            //默认控件高度
    private int DEFAULT_TEXT_SIZE = 50;                                                             //默认文字大小
    private int DEFAULT_RX = 10;                                                                    //默认圆角X坐标
    private int DEFAULT_RY = 10;                                                                    //默认圆角Y坐标
    //Context不能少，不然直接崩溃..
    Context mContext;

    @SuppressLint("ClickableViewAccessibility")
    public MyFilletRecView(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                Log.d(TAG, "按下");
                recBgColor = DEFAULT_REC_PRESSED_BG_COLOR;
                //重绘
                invalidate();
                return true;
            }
            case MotionEvent.ACTION_UP: {
                Log.d(TAG, "抬起");
                recBgColor = DEFAULT_REC_BG_COLOR;
                invalidate();
                return true;
            }
        }
        return false;

    }

    public MyFilletRecView(Context context, AttributeSet attrs) {
        //Constructor that is called when inflating a view from XML
        super(context, attrs);
        mContext = context;
    }

    //设置属性
    private int recBorderColor = getResources().getColor(R.color.black);//圆角矩形边框颜色
    private int recBgColor = DEFAULT_REC_BG_COLOR;                      //圆角矩形内部填充颜色
    private int textColor = DEFAULT_TAXT_COLOR;                         //文字颜色
    private int width = DEFAULT_REC_WIDTH, height = DEFAULT_REC_HEIGHT;
    private float paintWidth = 3f;                                      //默认画笔宽度

    private float textSize = DEFAULT_TEXT_SIZE;
    private String textContent = "zdd";
    private float Rx = DEFAULT_RX, Ry = DEFAULT_RY;                     //圆角x坐标及y坐标，当x等于宽度一半，y等于高度一半时，控件变成椭圆

    /**
     * 设置各部分颜色
     *
     * @param recBgColor 圆角矩形内部填充颜色
     * @param textColor  文字颜色
     * @return
     */
    public MyFilletRecView setColor(int recBgColor, int textColor) {
        this.recBgColor = recBgColor;
        this.textColor = textColor;
        return this;
    }

    /**
     * 设置内部文字内容
     *
     * @param textContent
     * @return
     */
    public MyFilletRecView setText(String textContent) {
        this.textContent = textContent;
        return this;
    }

    /**
     * 设置圆角矩形的宽度和高度
     *
     * @param with
     * @param height
     * @return
     */
    public MyFilletRecView setWidthAndHeight(int with, int height) {
        this.width = dp2px(with);
        this.height = dp2px(height);
        return this;
    }

    private int dp2px(float dp) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * 设置圆角矩形四个角的角度
     *
     * @param Rx 用于圆角的椭圆的X半径
     * @param Ry 用于圆角的椭圆的Y半径
     * @return
     */
    public MyFilletRecView setAngle(float Rx, float Ry) {
        this.Rx = Rx;
        this.Ry = Ry;
        return this;
    }

    public MyFilletRecView setTextSize(int textSize) {
        this.textSize = textSize;
        return this;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    // 1.创建一个画笔
    private Paint mPaint = new Paint();
    private Paint mPaint2 = new Paint();
    private Paint textPaint = new Paint();

    // 2.初始化画笔
    private void initFilletRecPaint(float width) {
        mPaint.setColor(recBgColor);                        //设置画笔颜色
        mPaint.setStyle(Paint.Style.FILL);                  //设置画笔模式为填充
        mPaint.setStrokeWidth(width);                       //设置画笔宽度为10px
    }

    private void initTextPaint() {
        //画文字
        textPaint.setColor(textColor);                      // 设置颜色
        textPaint.setStyle(Paint.Style.FILL);               // 设置样式
        textPaint.setTextSize(textSize);                    // 设置字体大小
        textPaint.setTextAlign(Paint.Align.CENTER);         //Paint设置水平居中
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画圆角矩形
        initFilletRecPaint(paintWidth);
        RectF rectF = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rectF, dp2px(Rx), dp2px(Ry), mPaint);

        //画文字
        initTextPaint();
        //参数分别为 (文本 基线x 基线y 画笔,其中xy指文字左下角坐标)
        canvas.drawText(textContent, width / 2, (height + textSize) / 2, textPaint);
    }
}
